﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;

namespace Fabrikam.Client.Converters
{
    public class StatusConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            Int32 state = (Int32)value;

            switch (state)
            {
                case 1:
                    return (new Uri("ms-appx:///Assets/Denied.png", UriKind.Absolute));
                case 2:
                    return (new Uri("ms-appx:///Assets/Approved.png", UriKind.Absolute));
                default:
                    return (new Uri("ms-appx:///Assets/None.png", UriKind.Absolute));
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null)
                return string.Empty;

            DateTime v = (DateTime)value;

            return v.ToString(DateTimeFormatInfo.CurrentInfo.ShortDatePattern);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class DateTimeToDateTimeOffsetConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value == null)
                return null;

            DateTime? dto = (DateTime?)value;

            if (!dto.HasValue)
                return null;

            return new DateTimeOffset(dto.Value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            if (value == null)
                return null;

            DateTimeOffset dt = (DateTimeOffset)value;

            return dt.Date;
        }
    }

    public class EscalationLevelConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }


    public class ColorLevelConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            Int32 state = (Int32)value;

            switch (state)
            {
                case 1:
                    return new SolidColorBrush(Color.FromArgb(255, 223, 0, 36));
                case 2:
                    return new SolidColorBrush(Color.FromArgb(255, 0, 159, 60));
                default:
                    return new SolidColorBrush(Color.FromArgb(255, 43, 118, 205));
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }

    public class StringFormatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return parameter == null ? value : String.Format((String)parameter, value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value;
        }
    }

    public class BooleanToVisibilityConverter : IValueConverter
    {
        private bool _inverted = false;

        public bool Inverted
        {
            get { return _inverted; }
            set { _inverted = value; }
        }



        private bool _not = false;

        public bool Not
        {
            get { return _not; }
            set { _not = value; }
        }



        private object VisibilityToBool(object value)
        {

            if (!(value is Visibility))
                return DependencyProperty.UnsetValue;

            return (((Visibility)value) == Visibility.Visible) ^ Not;
        }



        private object BoolToVisibility(object value)
        {

            if (!(value is bool))
                return DependencyProperty.UnsetValue;

            var val  = ((bool)value ^ Not) ? Visibility.Visible : Visibility.Collapsed;

            return val;
        }


        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return Inverted ? BoolToVisibility(value) : VisibilityToBool(value);

        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return Inverted ? VisibilityToBool(value) : BoolToVisibility(value);
        }
    }


}
